package com.ccx.coachsite.ui.share;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.CustomInfo;
import com.ccx.coachsite.bus.ExpertProvider;
import com.ccx.coachsite.bus.ManageInfo;
import com.ccx.coachsite.bus.ShareGroupInfo;
import com.ccx.coachsite.ui.AppBaseView;
import com.ccx.coachsite.ui.CommonAppView;
import com.ccx.coachsite.ui.manage.ManageKey;
import com.ccx.coachsite.ui.manage.ManageProvider;
import com.ccx.coachsite.ui.share.MobilePhoneView.OnBackListener;
import com.ccx.coachsite.util.StringUtil;

/**
 * 我的客户View Tab
 */
public class MyCustomerView extends AppBaseView {

	private EditText edtSearch;
	private View btnSend; 
	private Context mContext;
	private List<String> inputs;
	//private CheckBox mCheckbox;
	private ExpandableListView lstCustom;
	//private MyCustomListAdapter mMyCustomListAdapter;
	private MyCustomGroupAdapter mMyCustomGroupAdapter;
	//private ManageInfo mApp;
	private List<ShareGroupInfo> mApp;
	private List<ShareGroupInfo> mDataList;
	private ImageButton btnCheck;
	private boolean isCheck;
	private boolean isAllCustom;
	private TextView noTitleView;
	private OpenSendListener mOpenSend;
	private View customView;
	private View btnBack;
	
	public MyCustomerView(Context paramContext, boolean isAllCustom) {
		super(paramContext);
		
		mContext = paramContext;
		//addView(R.layout.share_my_custom);
		customView = addChild(R.layout.share_my_custom);
		edtSearch = (EditText)findViewById(R.id.share_custom_search_text);
		btnSend = findViewById(R.id.share_button_custom_send);
		noTitleView = (TextView)findViewById(R.id.share_custom_no_title);
		//mCheckbox = (CheckBox)findViewById(R.id.share_custom_checkbox);
		lstCustom = (ExpandableListView)findViewById(R.id.share_list_custom);
		btnCheck = (ImageButton)findViewById(R.id.share_custom_checkbox_image);
		btnCheck.setOnClickListener(new OnCheckListener());
		btnSend.setOnClickListener(new OnSendListener());
		inputs = new ArrayList<String>();
		isCheck = false;
		this.isAllCustom = isAllCustom;
		
		btnBack = findViewById(R.id.share_button_custom_back);
		btnBack.setOnClickListener(new OnBackListener());
		
		edtSearch.addTextChangedListener(new TextWatcher()
		{

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				doSearch();
				
			}
			
		});
		
		refreshData();
		//loadData();
	}
	
	class OnBackListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			Activity act = (Activity)mContext;
			if(act != null)
				act.onBackPressed();
		}
	}
	
	@Override
	protected boolean onLoadData() {
		mApp = ManageProvider.getInstance().getShareGroup(mContext);
		if (mApp != null)
		{
			doRefreshDataSuccess();
			return true;
		}
		return false;
	}

	@Override
	protected void onRefreshDataSuccess() {
		super.onRefreshDataSuccess();
		doRefreshDataSuccess();
		switchView(customView);
	}
	
	private List<ShareGroupInfo> getCustoms(String key)
	{
		List<ShareGroupInfo> mTmp = new ArrayList<ShareGroupInfo>();
		if(mApp == null)
			return mApp;
		for(int i = 0; i < mApp.size(); i++)
		{
			if(isAllCustom)
			{
				if(StringUtil.isEmpty(key))
				{
					if(mApp.get(i).getCustoms() != null && mApp.get(i).getCustoms().size() > 0)
						mTmp.add(mApp.get(i));
				}
				else
				{
					List<CustomInfo> mCust = new ArrayList<CustomInfo>();
					ShareGroupInfo sgi = new ShareGroupInfo();
					sgi.setGroupName(mApp.get(i).getGroupName());
					sgi.setCustoms(mCust);
					if(mApp.get(i).getCustoms() != null && mApp.get(i).getCustoms().size() > 0)
					{
						for(int j = 0; j < mApp.get(i).getCustoms().size(); j++)
						{
							CustomInfo cinfo = mApp.get(i).getCustoms().get(j);
							if(cinfo.getTianYi().indexOf(key) >= 0)
							{
								mCust.add(cinfo);
							}
						}	
					}
					if(mCust.size() > 0)
					{
						mTmp.add(sgi);
					}
				}
			}
			else
			{
				ShareGroupInfo sgi = new ShareGroupInfo();
				List<CustomInfo> mCust = new ArrayList<CustomInfo>();
				sgi.setGroupName(mApp.get(i).getGroupName());
				sgi.setCustoms(mCust);
				if(mApp.get(i).getCustoms() != null && mApp.get(i).getCustoms().size() > 0)
				{
					for(int j = 0; j < mApp.get(i).getCustoms().size(); j++)
					{
						CustomInfo cinfo = mApp.get(i).getCustoms().get(j);
						if(cinfo.isInvited())
						{
							if(StringUtil.isEmpty(key))
								mCust.add(cinfo);
							else if(cinfo.getTianYi().indexOf(key) >= 0)
								mCust.add(cinfo);
						}
					}
				}
				if(mCust.size() > 0)
				{
					mTmp.add(sgi);
				}
			}
		}

		
		return mTmp;
	}
	
	public void setOpenSendListener(OpenSendListener openSend){
		mOpenSend = openSend;
	}
	
	private void doSearch()
	{
		if(isBusy)
		{
			Toast.makeText(mContext, "数据正在加载中，请稍后重试", Toast.LENGTH_SHORT).show();
			return;
		}
		
		//if(mDataList == null || mDataList.size() == 0)
		//	return;
		
		String keys = edtSearch.getText().toString().trim();
		boolean isdata = true;
		if(StringUtil.isEmpty(keys))
		{
			mDataList = getCustoms(null);
			isdata = true;
		}
		else
		{
			mDataList = getCustoms(keys);
		}
		
		if(isdata)
		{
			lstCustom.setVisibility(View.VISIBLE);
			noTitleView.setVisibility(View.GONE);
		}
		else
		{
			lstCustom.setVisibility(View.GONE);
			noTitleView.setVisibility(View.VISIBLE);
			noTitleView.setText("没有找到符合的客户");
		}
		
		mMyCustomGroupAdapter.resetList(mDataList, isCheck);
		if(mDataList != null && mDataList.size() > 0)
		{
			int groupCount = mMyCustomGroupAdapter.getGroupCount();
		
			for (int i=0; i<groupCount; i++) {
				lstCustom.expandGroup(i);
			}
		}
	}
	
//	private void loadData()
//	{
//		mApp = ManageProvider.getInstance().getManageInfo(mContext);
//		if (mApp != null)
//		{
//			doRefreshDataSuccess();
//		}
//	}
//	
	protected void doRefreshDataSuccess() {
		mDataList = getCustoms(null);
		mMyCustomGroupAdapter = new MyCustomGroupAdapter(mContext, mDataList);
		
		lstCustom.setChildDivider(mContext.getResources().getDrawable(R.drawable.ower_sip_line));
		lstCustom.setAdapter(mMyCustomGroupAdapter);
		lstCustom.setGroupIndicator(null);
		int groupCount = mMyCustomGroupAdapter.getGroupCount();
		
		
		for (int i=0; i<groupCount; i++) {
			lstCustom.expandGroup(i);
		}
		
//		lstCustom.setDivider(mContext.getResources().getDrawable(R.drawable.ower_sip_line));
//		lstCustom.setAdapter(mMyCustomListAdapter);
//		lstCustom.setOnItemClickListener(new OnItemClickListener());
		
		lstCustom.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            public boolean onChildClick(ExpandableListView parent,
                            View v, int groupPosition, int childPosition,
                            long id) {
            	
				if(mMyCustomGroupAdapter != null)
					mMyCustomGroupAdapter.doSelectAtIndex(groupPosition, childPosition);
            	return true;
            }
		});
		
		boolean isdata = mDataList.size() > 0;
		if(isdata)
		{
			lstCustom.setVisibility(View.VISIBLE);
			noTitleView.setVisibility(View.GONE);
		}
		else
		{
			lstCustom.setVisibility(View.GONE);
			noTitleView.setVisibility(View.VISIBLE);
			noTitleView.setText("当前没有已加入应用助理的客户");
		}
	}
	
	private class OnItemClickListener implements android.widget.AdapterView.OnItemClickListener{

		@Override
		public void onItemClick(AdapterView<?> adapter, View v, int pos, long arg3) {
			if(pos < mDataList.size())
			{
//				if(mMyCustomListAdapter != null)
//					mMyCustomListAdapter.doSelectAtIndex(pos);
			}
		}
	}
	
	private boolean checkInput()
	{	
		if(mDataList == null || mDataList.size() == 0)
		{
			Toast.makeText(mContext, "非常抱歉，您当前没有客户。", Toast.LENGTH_SHORT).show();
			return false;
		}
		
		inputs = mMyCustomGroupAdapter.getInputs();
		if(inputs == null || inputs.size() == 0)
		{
			Toast.makeText(mContext, "请您至少选择一个客户", Toast.LENGTH_SHORT).show();
			return false;
		}
		
		return true;
	}
	
	private void send()
	{
		if (mOpenSend!=null)
			mOpenSend.sendSMS(2, OpenShareDetail.getShareUserIds(inputs));
	}
	
	class OnCheckListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if(isBusy)
			{
				Toast.makeText(mContext, "数据正在加载中，请稍后重试", Toast.LENGTH_SHORT).show();
				return;
			}
			
			isCheck = !isCheck;
			if(!isCheck)
				btnCheck.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_unchecked));
			else
				btnCheck.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_checked));
			mMyCustomGroupAdapter.doSelectAll(isCheck);
		}
	}
	
	class OnSendListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if(checkInput())
			{
				send();
			}
		}
	}

}
